import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:test01/okhttp_test/pojo/attention_pojo.dart';
import 'package:test01/okhttp_test/pojo/userinfor_pojo.dart';
import 'package:test01/okhttp_test/util/request_http.dart';

/*
*
*
* 我的-----关注界面
*
*
* */

class MyAttentionPage extends StatefulWidget {
  const MyAttentionPage({Key key}) : super(key: key);

  @override
  _MyAttentionPageState createState() => _MyAttentionPageState();
}

class _MyAttentionPageState extends State<MyAttentionPage> {
  Attention attentions = Attention();


  //列表数据
  List<AttentionInformation> attentionsList = [];
  //搜索的内容
  String searchValue;


  @override
  void initState() {
    attentionApi();
    super.initState();
  }

  ///调用关注用户列表接口
  attentionApi() async {
    Attention attention = await QueryFollowingList(
        size: 10, interface: "/appuser/web/user/follow/QueryFollowingList");
    for (int i = 0; i < attention.list.length; i++) {
      if (attention.list[i].relation == 1 || attention.list[i].relation == 3) {
        attentionsList.add(attention.list[i]);
      }
    }
    attention = attentions;
    setState(() {});
  }

  ///搜索接口
  searchApi(String searchValue) async{
    attentionsList=[];
    Attention attention = await searchFollowUser(interface:"/appuser/web/user/searchFollowUser",key:searchValue,size:10);
    if(attention.list.length!=0){
      for (int i = 0; i < attention.list.length; i++) {
        if (attention.list[i].relation == 1 || attention.list[i].relation == 3) {
          attentionsList.add(attention.list[i]);
        }
      }
    }
    attention = attentions;
    setState(() {});
  }

  ///搜索
  Widget search() {
    return Container(
        width: MediaQuery.of(context).size.width - 32,
        height: 32,
        margin: EdgeInsets.only(top: 18, left: 16),
        decoration: BoxDecoration(
          color: Color.fromRGBO(243, 243, 243, 0.65),
          borderRadius: BorderRadius.circular(3),
        ),
        child: Container(
          margin: EdgeInsets.only(left: 12),
          child: TextField(
            cursorColor: Color.fromRGBO(153, 153, 153, 1),
            maxLines: 1,
            decoration: InputDecoration(
              icon: Icon(Icons.zoom_out_rounded,
                  size: 21, color: Color.fromRGBO(153, 153, 153, 1)),
              hintText: '搜索用户',
              hintStyle: TextStyle(
                  color: Color.fromRGBO(153, 153, 153, 1), fontSize: 16),
              //设置输入框无边框
              // border: InputBorder.none,
              //设置边框内容居中
              focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              disabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              border: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              contentPadding: EdgeInsets.symmetric(vertical: 0),
            ),

            onChanged: (value){
              print("onChanged的监听方法：$value");
              searchApi(value);
            },
          ),
        ));
  }

  ///所有关注的话题
  Widget alltopics() {
    return Container(
      height: 48,
      child: Row(
        children: <Widget>[
          Container(
            height: 38,
            width: 38,
            padding: EdgeInsets.all(9.5),
            margin: EdgeInsets.only(left: 16),
            child: Icon(
              Icons.ac_unit,
              size: 19.12,
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 12),
            child: Text(
              "这里是我关注的所有话题",
              style:
                  TextStyle(fontSize: 15, color: Color.fromRGBO(17, 17, 17, 1)),
            ),
          ),
          Spacer(),
          Container(
            child: Icon(
              Icons.arrow_forward_ios_sharp,
              size: 18,
              color: Color.fromRGBO(112, 112, 112, 1),
            ),
          ),
          SizedBox(
            width: 16,
          )
        ],
      ),
    );
  }

  ///关注列表
  Widget followitem(int i) {
    return Container(
      height: 48,
      width: MediaQuery.of(context).size.width,
      margin: EdgeInsets.only(top: 10),
      child: Row(
        children: <Widget>[
          Container(
            height: 38,
            width: 38,
            margin: EdgeInsets.only(left: 16),
            child: ClipOval(
              child: Image.network(
                attentionsList[i].avatarUri,
                fit: BoxFit.cover,
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 12),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width - 65 - 59,
                  child: Text(
                    attentionsList[i].nickName,
                    style: TextStyle(fontSize: 15, color: Colors.black),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),

                ///判断是否为空
                (attentionsList[i].description) != null
                    ? Container(
                        width: MediaQuery.of(context).size.width - 65 - 59,
                        child: Text(
                          attentionsList[i].description,
                          style: TextStyle(
                              fontSize: 12,
                              color: Color.fromRGBO(153, 153, 153, 1)),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      )
                    : Container(),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        ///去掉APPBar的箭头
        automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
        elevation: 0,
        title: Row(
          children: <Widget>[
            GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Container(
                // margin: EdgeInsets.only(left: 16),
                child: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.black,
                  size: 24,
                ),
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width - 72 - 72,
              alignment: Alignment.center,
              margin: EdgeInsets.only(left: 32),
              child: Text(
                "我的关注",
                style: TextStyle(
                    fontSize: 18, color: Color.fromRGBO(17, 17, 17, 1)),
              ),
            )
          ],
        ),
      ),
      body: SingleChildScrollView(
        child: attentions.hasNext != 0
            ? Column(
                children: <Widget>[
                  //搜索
                  search(),
                  alltopics(),
                  attentionsList.isEmpty?
                  Container():Container(
                      margin: EdgeInsets.only(top: 12),
                      height: MediaQuery.of(context).size.height - 62 - 48 - 48,
                      child:ListView.builder(
                        reverse: false,
                        itemBuilder: (BuildContext context, int i) {
                          return followitem(i);
                        },
                        itemCount: attentionsList.length,
                      )
                  )
                ],
              )
            : Container()
          ),
    );
  }
}
